package com.zhongwang.mms.module.half.service;

import com.zhongwang.mms.gen.entity.HInventory;
import com.zhongwang.mms.gen.entity.TWarehouseInfo;
import com.zhongwang.mms.gen.entity.TWarehouseInfoExample;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.basic.dao.TWarehouseInfoDao;
import com.zhongwang.mms.module.half.dao.HInventoryDao;
import com.zhongwang.mms.module.half.dao.HOrderDetailDao;
import com.zhongwang.mms.module.half.dao.HalfInveAdjDao;
import com.zhongwang.mms.module.half.model.HInventoryModel;
import com.zhongwang.mms.module.half.model.HOrderDetailModel;
import com.zhongwang.mms.module.half.model.HalfInveAdjModel;
import com.zhongwang.mms.module.overview.model.ProjectStatusModel;
import com.zhongwang.mms.module.system.log.LogService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *库存调整
 *
 */
@Service
public class HalfInveAdjService {
    @Autowired
    private HalfInveAdjDao halfInveAdjDao;
    @Autowired
    private HInventoryDao hInventoryDao;
    @Autowired
    private HOrderDetailDao hOrderDetailDao;
    @Autowired
    private TWarehouseInfoDao tWarehouseInfoDao;
    @Autowired
    private LogService logService;

    /**
     *查询调整页面
     * @param hAdjustmentApplyDetail 调整列表查询条件
     * @return 库存调整列表
     */
    public List<HalfInveAdjModel> findAdjList(HalfInveAdjModel hAdjustmentApplyDetail) {
        return halfInveAdjDao.selectHalfInveAdj(hAdjustmentApplyDetail);
    }

    /**
     * 库存列表
     * @param hInventory 库存列表查询条件
     * @return 库存列表
     */
    public List<HInventoryModel> findInveList(HInventoryModel hInventory) {
        return hInventoryDao.selectHalfInve(hInventory);
    }

    /**
     * 根据id查找库存
     * @param id 库存id
     * @return 库存信息
     */
    public HInventoryModel findHInventoryById(Integer id) {
        return hInventoryDao.selectHalfInveById(id);
    }

    /**
     * 保存库存调整
     * 调整库存库存信息或调整库存数量
     * 调整库存信息生成2条调整记录 1：A物料 x -> 0; 2：B物料 0 -> x;
     * 数量调整生成1条调整记录 A物料 x -> y
     * @param hInventory 前端返回的库存信息
     * @return 插入的调整单行数
     */
    public int halInvSave(HInventoryModel hInventory) {
        int row = 0;
//        查找库存调整表中是否已存在此条库存的调整，如果存在未提交、未审核的调整则不能保存
        List<HalfInveAdjModel> halfInveAdjModelId = halfInveAdjDao.selectHalfInveAdjModelId(hInventory.getItyId());
        if(halfInveAdjModelId.size() > 0) {
            row = 0;
        }else {
            HInventoryModel inventory = hInventoryDao.selectHalfInveById(hInventory.getItyId());//查询库存信息
            Session session = SecurityUtils.getSubject().getSession();
            User user = (User) session.getAttribute("user");
            //为页面传回的字符串型去空格
            if (hInventory.getItyMatName().trim() .equals("")){
                hInventory.setItyMatName(null);
            }
            if (hInventory.getItyUnit().trim().equals("")){
                hInventory.setItyUnit(null);
            }
            //比较库存和页面传回的物料描述、单位、单价是否相同，如果不相同则生成2条调整记录
            if(
               (hInventory.getItyMatName() != null && inventory.getItyMatName() !=null && !hInventory.getItyMatName().equals(inventory.getItyMatName())) ||
               (hInventory.getItyMatName() == null && inventory.getItyMatName() !=null) ||
               (hInventory.getItyMatName() != null && inventory.getItyMatName() ==null) ||
               (hInventory.getItyUnit() != null && inventory.getItyUnit() != null && !hInventory.getItyUnit().equals(inventory.getItyUnit()))||
               (hInventory.getItyUnit() == null && inventory.getItyUnit() != null) ||
               (hInventory.getItyUnit() != null && inventory.getItyUnit() == null) ||
               (hInventory.getItyPrice() != null && inventory.getItyPrice() != null && !hInventory.getItyPrice().equals(inventory.getItyPrice()))||
               (hInventory.getItyPrice() == null && inventory.getItyPrice() != null) ||
               (hInventory.getItyPrice() != null && inventory.getItyPrice() == null)) {
                row = hAdjEditInfo(inventory, hInventory);
            }//更改数量的库存调整，生成一条调整记录
            else if(!hInventory.getItyNum().equals(inventory.getItyNum())){
                row = hAdjEditNum(inventory, hInventory);
            } else row = -1;
        }
        return row;
    }

    /**
     * 调整库存数量调用此方法
     * 数量调整生成1条调整记录 A物料 x -> y
     * @param inventory 实际库存
     * @param hInventory 前端返回库存调整信息
     * @return 新增行数
     */
    private int hAdjEditNum(HInventoryModel inventory, HInventoryModel hInventory) {
        int row = 0;
        HalfInveAdjModel hAdjustmentApplyDetail = hAdjustmentApplyUpdateNum(inventory, hInventory);
        halfInveAdjDao.insertInveAdj(hAdjustmentApplyDetail);
        logService.saveLog("成品库：库存调整新增", hInventory.getItyMatName(),hInventory.getItyNum());
        row ++;
        return row;
    }

    /**
     * 更改库存物料信息，生成2条调整记录
     * 调整库存物料信息调用此方法
     * 调整库存信息生成2条调整记录 1：A物料 x -> 0; 2：B物料 0 -> x;
     * @param inventory 实际库存
     * @param hInventory 前端返回库存调整信息
     * @return 插入调整单行数
     */
    private int hAdjEditInfo(HInventoryModel inventory, HInventoryModel hInventory) {
        int row = 0;
        //库存调整表插入一条，更改前物料减少为0的记录
        HalfInveAdjModel hAdjustmentApplyDetail = hAdjustmentApplyInsertDec(inventory, hInventory);
        halfInveAdjDao.insertInveAdj(hAdjustmentApplyDetail);
        logService.saveLog("成品库：库存调整", hInventory.getItyMatName(),hInventory.getItyNum());
        row++;
        //库存调整表插入一条，更改后物料从0增加到更改后数量的记录
        hAdjustmentApplyDetail = hAdjustmentApplyInsertInc(hInventory, hAdjustmentApplyDetail);
        halfInveAdjDao.insertInveAdj(hAdjustmentApplyDetail);
        logService.saveLog("成品库：库存调整新增", hInventory.getItyMatName(),hInventory.getItyNum());
        row++;
        return row;
    }


    /**
     * 打开查看库存调整模态框，按库存id查找调整信息
     * 从调整列表，选择查看调整信息
     * 首先查找调整单状态未被置为0的调整单号，是否有2单相同，如果有2单相同，则查找另一单的信息。调整前显示aadBeAfFlag字段为1的before信息；调整后显示aadBeAfFlag字段为2的after信息
     * 如果没有相同没有相同的调整单号，则只查看本调整单的After(调整后)信息即可。
     * @param id 前端传回调整单id
     * @return 调整单信息
     */
    public HalfInveAdjModel viewInvAdjById(Integer id) {
        HalfInveAdjModel halfInveAdj1 = halfInveAdjDao.selectHalfInveAdjById(id);
        //查找未提交的库存调整单是否有两个相同单号
        List<HalfInveAdjModel>list = halfInveAdjDao.selectAdjSameOrderByOrder(halfInveAdj1.getAadAdjOrderCode());
        if (list.size() == 2){
            //如果halfInveAdj1为前单
            if(halfInveAdj1.getAadBeAfFlag() == 1){
                //查找后单
                int aadBeAfFlag = 2;
                HalfInveAdjModel halfInveAdj2= halfInveAdjDao.selectAdjSameOrderByOrder1(halfInveAdj1.getAadAdjOrderCode(), aadBeAfFlag);
                halfInveAdj1.setAadAfterMatCode(halfInveAdj2.getAadAfterMatCode());
                halfInveAdj1.setAadAfterMatName(halfInveAdj2.getAadAfterMatName());
                halfInveAdj1.setAadAfterProjectCode(halfInveAdj2.getAadAfterProjectCode());
                halfInveAdj1.setAadAfterProjectName(halfInveAdj2.getAadAfterProjectName());
                halfInveAdj1.setAadAfterPart(halfInveAdj2.getAadAfterPart());
                halfInveAdj1.setAadAfterNum(halfInveAdj2.getAadAfterNum());
                halfInveAdj1.setAadAfterTrackCode(halfInveAdj2.getAadAfterTrackCode());
                halfInveAdj1.setAadAfterUnit(halfInveAdj2.getAadAfterUnit());
                halfInveAdj1.setAadAfterPrice(halfInveAdj2.getAadAfterPrice());
                halfInveAdj1.setAadAfterBrand(halfInveAdj2.getAadAfterBrand());
                halfInveAdj1.setAadAfterWhiId(halfInveAdj2.getAadAfterWhiId());
                halfInveAdj1.setAadAfterWhiName(halfInveAdj2.getAadAfterWhiName());
                halfInveAdj1.setAadAfterPosition(halfInveAdj2.getAadAfterPosition());
                halfInveAdj1.setAadAfterSapMatCode(halfInveAdj2.getAadAfterSapMatCode());
                halfInveAdj1.setAadAfterRemark(halfInveAdj2.getAadAfterRemark());
                halfInveAdj1.setAadAfterDrawingNo(halfInveAdj2.getAadAfterDrawingNo());
//                halfInveAdj1.setAadAfterMakingPiecesStatus(halfInveAdj2.getAadAfterMakingPiecesStatus());
//                halfInveAdj1.setAadAfterIfFinishedProduct(halfInveAdj2.getAadAfterIfFinishedProduct());
                halfInveAdj1.setAadAfterCaseNo(halfInveAdj2.getAadAfterCaseNo());
                halfInveAdj1.setAadAfterBoxingDate(halfInveAdj2.getAadAfterBoxingDate());
                halfInveAdj1.setAadAfterStockType(halfInveAdj2.getAadAfterStockType());


            }//如果halfInveAdj1为后单
            else if(halfInveAdj1.getAadBeAfFlag() == 2){
                //查找前单
                int aadBeAfFlag = 1;
                HalfInveAdjModel halfInveAdj2= halfInveAdjDao.selectAdjSameOrderByOrder1(halfInveAdj1.getAadAdjOrderCode(), aadBeAfFlag);
                halfInveAdj1.setAadBeforeMatCode(halfInveAdj2.getAadBeforeMatCode());
                halfInveAdj1.setAadBeforeMatCode(halfInveAdj2.getAadBeforeMatCode());
                halfInveAdj1.setAadBeforeMatName(halfInveAdj2.getAadBeforeMatName());
                halfInveAdj1.setAadBeforeProjectCode(halfInveAdj2.getAadBeforeProjectCode());
                halfInveAdj1.setAadBeforeProjectName(halfInveAdj2.getAadBeforeProjectName());
                halfInveAdj1.setAadBeforePart(halfInveAdj2.getAadBeforePart());
                halfInveAdj1.setAadBeforeNum(halfInveAdj2.getAadBeforeNum());
                halfInveAdj1.setAadBeforeTrackCode(halfInveAdj2.getAadBeforeTrackCode());
                halfInveAdj1.setAadBeforeUnit(halfInveAdj2.getAadBeforeUnit());
                halfInveAdj1.setAadBeforePrice(halfInveAdj2.getAadBeforePrice());
                halfInveAdj1.setAadBeforeBrand(halfInveAdj2.getAadBeforeBrand());
                halfInveAdj1.setAadBeforeWhiId(halfInveAdj2.getAadBeforeWhiId());
                halfInveAdj1.setAadAfterWhiName(halfInveAdj2.getAadBeforeWhiName());
                halfInveAdj1.setAadBeforePosition(halfInveAdj2.getAadBeforePosition());
                halfInveAdj1.setAadBeforeSapMatCode(halfInveAdj2.getAadBeforeSapMatCode());
                halfInveAdj1.setAadBeforeRemark(halfInveAdj2.getAadBeforeRemark());
                halfInveAdj1.setAadBeforeDrawingNo(halfInveAdj2.getAadBeforeDrawingNo());
//                halfInveAdj1.setAadBeforeMakingPiecesStatus(halfInveAdj2.getAadBeforeMakingPiecesStatus());
//                halfInveAdj1.setAadBeforeIfFinishedProduct(halfInveAdj2.getAadBeforeIfFinishedProduct());
                halfInveAdj1.setAadBeforeCaseNo(halfInveAdj2.getAadBeforeCaseNo());
                halfInveAdj1.setAadBeforeBoxingDate(halfInveAdj2.getAadBeforeBoxingDate());
                halfInveAdj1.setAadBeforeStockType(halfInveAdj2.getAadBeforeStockType());
            }
        }else {
            halfInveAdj1 =  halfInveAdjDao.selectHalfInveAdjById(id);;
        }
        return  halfInveAdj1;
    }

    /**
     * 保存修改库存调整
     * 已添加的库存调整，在提交待审前可修改
     * 只能修改数量，如果更改其他，请删除重新添加库存调整
     * @param hAdjustmentApplyDetail 前端返回的库存调整信息
     * @return 修改库存条数
     */
    public int halInvEditSave(HalfInveAdjModel hAdjustmentApplyDetail) {
        int row = 0;
        //页面传回的物料描述和单位去空格
        if (hAdjustmentApplyDetail.getAadAfterMatName().trim() .equals("")){
            hAdjustmentApplyDetail.setAadAfterMatName(null);
        }
        if (hAdjustmentApplyDetail.getAadAfterUnit().trim().equals("")){
            hAdjustmentApplyDetail.setAadAfterUnit(null);
        }
        //如果改物料描述、单位、单价，返回row=0，提醒删除重新添加调整
       HalfInveAdjModel halfInveAdjModel = halfInveAdjDao.selectHalfInveAdjById(hAdjustmentApplyDetail.getAadId());
        if(
            (hAdjustmentApplyDetail.getAadAfterMatName() != null && halfInveAdjModel.getAadAfterMatName() !=null && !hAdjustmentApplyDetail.getAadAfterMatName().equals(halfInveAdjModel.getAadAfterMatName())) ||
            (hAdjustmentApplyDetail.getAadAfterMatName() == null && halfInveAdjModel.getAadAfterMatName() !=null) ||
            (hAdjustmentApplyDetail.getAadAfterMatName() != null && halfInveAdjModel.getAadAfterMatName() ==null) ||
            (hAdjustmentApplyDetail.getAadAfterUnit() != null && halfInveAdjModel.getAadAfterUnit() != null && !hAdjustmentApplyDetail.getAadAfterUnit().equals(halfInveAdjModel.getAadAfterUnit()))||
            (hAdjustmentApplyDetail.getAadAfterUnit() == null && halfInveAdjModel.getAadAfterUnit() != null) ||
            (hAdjustmentApplyDetail.getAadAfterUnit() != null && halfInveAdjModel.getAadAfterUnit() == null) ||
            (hAdjustmentApplyDetail.getAadAfterPrice() != null && halfInveAdjModel.getAadAfterPrice() != null && !hAdjustmentApplyDetail.getAadAfterPrice().equals(halfInveAdjModel.getAadAfterPrice()))||
            (hAdjustmentApplyDetail.getAadAfterPrice() == null && halfInveAdjModel.getAadAfterPrice() != null) ||
            (hAdjustmentApplyDetail.getAadAfterPrice() != null && halfInveAdjModel.getAadAfterPrice() == null))
        {
            return row;
        }//更新数量
        else if(!hAdjustmentApplyDetail.getAadAfterNum().equals(halfInveAdjModel.getAadAfterNum())){
            Session session = SecurityUtils.getSubject().getSession();
            User user = (User) session.getAttribute("user");
            hAdjustmentApplyDetail.setAadApplDate(new Date());
            hAdjustmentApplyDetail.setAadApplicant(user.getName());
            halfInveAdjDao.updateInvAdj(hAdjustmentApplyDetail);
            row++;
            logService.saveLog("成品库：编辑库存调整", hAdjustmentApplyDetail.getAadAfterMatName(),hAdjustmentApplyDetail.getAadAfterNum());
            return row;
        }else return -1;
    }

    /**
     * 修改库存调整
     * 打开修改调整库存的模态框时，按id查询库存调整
     * @param id 调整单id
     * @return 调整单信息
     */
    public HalfInveAdjModel findInvAdjById(Integer id) {
        HalfInveAdjModel halfInveAdj = halfInveAdjDao.selectHalfInveAdjById(id);
        return halfInveAdj;
    }

    /**
     * 删除调整申请
     * 调整单为提交待审前，可删除调整单
     * 将调整单信息置为0
     * @param hAdjustmentApplyDetail 调整单信息
     * @return 删除调整单条数
     */
    public int halInvDel(HalfInveAdjModel hAdjustmentApplyDetail) {
        hAdjustmentApplyDetail.setAadStatus(0);
        int row = 0;
        halfInveAdjDao.updateInvAdjStatus(hAdjustmentApplyDetail);
        row++;
        return row;
    }

    /**
     * 批量提交库存调整
     * 已添加的库存调整信息，提交后，进入待审核状态
     * 将调整单状态置为2
     * @param list 前端返回的调整单id列表
     * @return 已提交的调整单条数
     */
    public int submitAjust(List<String> list) {
        int row = 0;
        HalfInveAdjModel hAdjustmentApplyDetail;
        for(String id :list)
        {
            hAdjustmentApplyDetail = halfInveAdjDao.selectHalfInveAdjById(Integer.valueOf(id));
            hAdjustmentApplyDetail.setAadStatus(2);
            halfInveAdjDao.updateInvAdjStatus(hAdjustmentApplyDetail);
            row++;
        }
        logService.saveLog("成品库：提交库存调整申请", "批量提交数量：" + list.size() + " 条");
        return row;
    }

    /**
     * 新增库存调整保存
     * 库存调整时，不需要选择库存进行调整，直接新增库存时，调用此方法
     * @param hAdjustmentApplyDetail 前端返回的库存调整信息
     * @return 保存的库存调整行数
     */
    public int halInvCreateSave(HalfInveAdjModel hAdjustmentApplyDetail) {
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        //新增库存调整，调整后为页面数量
        hAdjustmentApplyDetail.setAadAfterMatCode("1111111111");
        hAdjustmentApplyDetail.setAadAfterMatName(hAdjustmentApplyDetail.getAadAfterMatName());
        hAdjustmentApplyDetail.setAadAfterSapMatCode(hAdjustmentApplyDetail.getAadAfterSapMatCode());
        hAdjustmentApplyDetail.setAadAfterProjectCode(hAdjustmentApplyDetail.getAadAfterProjectCode());
        hAdjustmentApplyDetail.setAadAfterProjectName(hAdjustmentApplyDetail.getAadAfterProjectName());
        hAdjustmentApplyDetail.setAadAfterPart(hAdjustmentApplyDetail.getAadAfterPart());
        hAdjustmentApplyDetail.setAadAfterNum(hAdjustmentApplyDetail.getAadAfterNum());
        hAdjustmentApplyDetail.setAadAfterTrackCode(hAdjustmentApplyDetail.getAadAfterTrackCode());
        hAdjustmentApplyDetail.setAadAfterUnit(hAdjustmentApplyDetail.getAadAfterUnit());
        hAdjustmentApplyDetail.setAadAfterPrice(hAdjustmentApplyDetail.getAadAfterPrice());
        hAdjustmentApplyDetail.setAadAfterBrand(hAdjustmentApplyDetail.getAadAfterBrand());
        hAdjustmentApplyDetail.setAadAfterWhiId(hAdjustmentApplyDetail.getAadAfterWhiId());
        hAdjustmentApplyDetail.setAadAfterPosition(hAdjustmentApplyDetail.getAadAfterPosition());
        hAdjustmentApplyDetail.setAadAfterSapMatCode(hAdjustmentApplyDetail.getAadAfterSapMatCode());
        hAdjustmentApplyDetail.setAadAfterRemark(hAdjustmentApplyDetail.getAadAfterRemark());
        hAdjustmentApplyDetail.setAadAfterDrawingNo(hAdjustmentApplyDetail.getAadAfterDrawingNo());
//        hAdjustmentApplyDetail.setAadAfterMakingPiecesStatus(hAdjustmentApplyDetail.getAadAfterMakingPiecesStatus());
//        hAdjustmentApplyDetail.setAadAfterIfFinishedProduct(hAdjustmentApplyDetail.getAadAfterIfFinishedProduct());
        hAdjustmentApplyDetail.setAadAfterMatType(hAdjustmentApplyDetail.getAadAfterMatType());
        hAdjustmentApplyDetail.setAadAfterCaseNo(hAdjustmentApplyDetail.getAadAfterCaseNo());
        hAdjustmentApplyDetail.setAadAfterBoxingDate(hAdjustmentApplyDetail.getAadAfterBoxingDate());
        hAdjustmentApplyDetail.setAadAfterStockType(hAdjustmentApplyDetail.getAadAfterStockType());
        //状态等赋值
        hAdjustmentApplyDetail.setAadAfterSpecialInstructions(hAdjustmentApplyDetail.getAadAfterSpecialInstructions());
        hAdjustmentApplyDetail.setAadStatus(1);
        hAdjustmentApplyDetail.setAadApplicant(user.getName());
        hAdjustmentApplyDetail.setAadApplDate(new Date());
        hAdjustmentApplyDetail.setAadAdjOrderCode(createAdjOrderCode());
        return halfInveAdjDao.insertInveAdj(hAdjustmentApplyDetail);
    }

    /**
     * 调整审核列表数据
     * @param hAdjustmentApplyDetail 查询条件
     * @return 库存调整和库存盘点列表数据
     */
    public List<HalfInveAdjModel> findAdjApprList(HalfInveAdjModel hAdjustmentApplyDetail) {
        return halfInveAdjDao.selectHalfInveAdjAppr(hAdjustmentApplyDetail);
    }

    /**
     * 审核不通过
     * 库存盘点和库存调整审核不通过调用此方法
     * @param id 调整id
     * @param adjStatus 调整类型 1 库存调整 2 库存盘点
     * @return 审核不通过条数
     */
    public int halInvAdjDisagree(@Param("Integer id") Integer id, @Param("Integer adjStatus")Integer adjStatus) {
        int row = 0;
        //库存调整
        if(adjStatus == 1){
            HalfInveAdjModel halfInveAdjModel = halfInveAdjDao.selectHalfInveAdjById(id);
            row = hAdjApprDisagree(halfInveAdjModel);
        }//库存盘点
        else if(adjStatus == 2){
            HalfInveAdjModel halfInveAdjModel = halfInveAdjDao.selectHalfInveCountById(id);
            row = hCountApprDisagree(halfInveAdjModel);
        }else row = 0;
        return row;
    }

    /**
     * 盘点审核不通过
     * 如果调整类型为2，盘点审核不通过调用此方法
     * @param halfInveAdjModel 盘点信息
     * @return 盘点不通过条数
     */
    private int hCountApprDisagree(HalfInveAdjModel halfInveAdjModel) {
        int row = 0;
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        halfInveAdjModel.setAadReviewDate(new Date());
        halfInveAdjModel.setAadReviewer(user.getName());
        halfInveAdjModel.setAadStatus(4);
        row = halfInveAdjDao.updateInvCountAppr(halfInveAdjModel);
        row++;
        return row;
    }

    /**
     * 调整审核不通过
     * 如果调整类型为1，调整审核不通过调用此方法
     * @param halfInveAdjModel 调整单信息
     * @return 调整单不通过的条数
     */
    private int hAdjApprDisagree(HalfInveAdjModel halfInveAdjModel) {
        int row = 0;
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        halfInveAdjModel.setAadReviewDate(new Date());
        halfInveAdjModel.setAadReviewer(user.getName());
        halfInveAdjModel.setAadStatus(4);
       // halfInveAdjDao.updateInvAdjAppr(halfInveAdjModel);
        halfInveAdjDao.updateInvAdjApprByAdjOrderCode(halfInveAdjModel);
        row++;
        return row;
    }

    /**
     * 单条审核通过
     * 页面点击审核通过按钮，进行单条审核通过
     * @param id 申请的调整id
     * @param adjStatus 调整类型 1库存调整 2库存盘点
     * @return 返回审核通过条数
     */
    public int halInvAdjAgree(@Param("Integer id") Integer id, @Param("Integer adjStatus")Integer adjStatus) {
        int row = 0;
        row = row + apprAgree(id, adjStatus);
        return row;
    }

    /**
     * 批量审核通过库存调整
     * 在审核库存调整申请页面，批量选择的库存调整，进行审核通过
     * @param halfInveAdjModel 前端返回的需要审核通过的调整信息，id和调整类型 1库存调整 2库存盘点
     * @return 已审核通过的条数
     */
    public int submitAjustAppr(List<HalfInveAdjModel> halfInveAdjModel) {
        int row = 0;
        for (HalfInveAdjModel hAdjustmentApplyDetailId : halfInveAdjModel) {
            int adjStatus= hAdjustmentApplyDetailId.getAdjStatus();
            int id = hAdjustmentApplyDetailId.getAadId();
            row = row + apprAgree(id, adjStatus);
        }
        return row;
    }

    /**
     * 单条和多条审核通过调用此方法
     * 首先判断是否是调整单类型1库存调整，2库存盘点
     *      如果是1库存调整，判断调整单库存外键ity_id是否为空。
     *          如果调整条库存外键ity_id为空，先查找是否是有调整单前单，如果有，看前单对应的库存在库数量和可用数量是否相等，不相等返回。
     *              否则进行插入出入库单、插入库存、更改调整单信息     *
     *          如果调整单库存外键ity_id不为空，先比较调整单对应的库存在库数量和可用数量是否相等，如果相等，
     *          则进行调整库存，比较调整单的before和after在库数量。
     *              如果after大于before则进行插入出入库单、按调整单库存外键ity_id更改库存、更改调整单审核信息
     *              如果after小于before，首先判断库存表中的在库数量是否比after-before的差值小，
     *                  如果小就不做审核；
     *                  如果不小，则进行审核，插入出入库单、按调整单库存外键ity_id更改库存、更改调整单审核信息
     *      如果是2库存盘点，先判断盘点是否有库存外键，
     *          如果有，先判断盘点对应的库存在库数量和可用数量是否相等，如果相等，再判断损溢值+在库数量是否大于0，如果大于0，则
     *                  则进行插入出入库单、按调整单库存外键ity_id更改库存、更改调整单审核信息，
     *          如果没有，则判断损溢值是否大于0，
     *                  如果大于0，则进行审核，插入出入库单、插入存库信息
     * @param id 调整单id
     * @param adjStatus 调整单类型
     * @return row 调整的条数
     */
    private int apprAgree(Integer id, Integer adjStatus){
        int count = 0; //审核通过计数

            //判断是库存调整or库存盘点
            if (adjStatus == 1)//如果是更改库存调整
            {
                HalfInveAdjModel hAdjustmentApply = halfInveAdjDao.selectHalfInveAdjById(id);
                HInventoryModel hInventory = hInventoryDao.selectHalfInveById(hAdjustmentApply.getAadItyId());
                if(hInventory == null)//无库存id,未查到相关库存,即不调整库存，新增库存
                {
                    int aadBeAfFlag = 1;
                    HalfInveAdjModel halfInveAdj2 = halfInveAdjDao.selectAdjSameOrderByOrder1(hAdjustmentApply.getAadAdjOrderCode(), aadBeAfFlag);//查询是否有相同的表单前单
                    if (halfInveAdj2 == null)//如果没有
                    {
                        count = hAdjNumApprAagree(hAdjustmentApply); //无库存id,新增
                    }else if(halfInveAdj2.getTempItyNum().equals(halfInveAdj2.getItyAvaNum()))//如果前单对应的在库库存和可用库存相等
                    {
                        count = hAdjNumApprAagree(hAdjustmentApply); //无库存id,新增
                    }else return count; //否则不执行
                }else  if (hAdjustmentApply.getAadItyId() != null){
                    if (hInventory.getItyNum().equals( hInventory.getItyAvaNum())){
                        //如果更改前在库数量小于修改后的数量；入库；查找原需要更改的库存id，改存库
                        if(hAdjustmentApply.getAadBeforeNum() < hAdjustmentApply.getAadAfterNum()){
                            count = hAdjApprAagree(hAdjustmentApply,hInventory);
                        }
                        //如果更改前在库数量大于修改后的数量；出库；查找原需要更改的库存id，改存库
                        else if (hAdjustmentApply.getAadBeforeNum() > hAdjustmentApply.getAadAfterNum()){
                            if (hInventory.getItyNum() < (hAdjustmentApply.getAadAfterNum()- hAdjustmentApply.getAadBeforeNum()))//如果库存实际在库数量小于调整量，不能审核
                            {
                                return count;
                            }else {
                                count = hAdjApprAagree(hAdjustmentApply,hInventory);
                            }
                        } else return count;
                    }else return count;
                }else return count;
            }else if(adjStatus == 2){
                HalfInveAdjModel hAdjustmentApply = halfInveAdjDao.selectHalfInveCountById(id);
                HInventoryModel hInventory = hInventoryDao.selectHalfInveById(hAdjustmentApply.getAadItyId());
                //如果库存外键不为空
                if(hInventory != null){
                    //如果损溢数量小于0，出库，按照id改库存
                    if(hInventory.getItyNum().equals(hInventory.getItyAvaNum())) {
                        if ((hInventory.getItyNum()+ hAdjustmentApply.getAjaDeOvNum()) < 0)//如果库存数量小于调整量,不审核
                        {
                            return count;
                        }else {
                            hCountApprAagree(hAdjustmentApply,hInventory);
                        }
                    }
                    //不满足条件返回row，未审核
                    else return count;
                } else if(hInventory == null)//如果依赖库存主键为空，则为新增的库存盘点
                {
                    if(hAdjustmentApply.getAjaDeOvNum() <= 0)//损溢值小于等于零不审核
                    {
                        return count;
                    }else if(hAdjustmentApply.getAjaDeOvNum() > 0)//损溢值大于零审核
                    {
                        hCountApprAagreeCreate(hAdjustmentApply);
                    }return count;
                }
                //不满足条件返回0，未审核
                else  {
                    return count;
                }
            }
            return count;
    }

    /**
     * 审核通过新增库存盘点调用此方法
     * 插入出入库子单，插入入库记录，更改审核状态
     * @param hAdjustmentApply 调整明细
     * @return 更改条数
     */
    private int hCountApprAagreeCreate(HalfInveAdjModel hAdjustmentApply) {
        int row = 0;
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        HInventory hInventoryCreate = new HInventory();
        //出入库子表插入出库信息
        HOrderDetailModel record = inserthOrderDetail(hAdjustmentApply);
        hOrderDetailDao.insert(record);
        //新增库存
        hInventoryCreate.setItyNum(Double.valueOf(hAdjustmentApply.getAjaDeOvNum()));
        hInventoryCreate.setItyAvaNum(Double.valueOf(hAdjustmentApply.getAjaDeOvNum()));
        hInventoryCreate.setItyRowNo(record.getOdaId());
        hInventoryCreate.setItyMatCode(hAdjustmentApply.getAadAfterMatCode());
        hInventoryCreate.setItyMatName(hAdjustmentApply.getAadAfterMatName());
        hInventoryCreate.setItyProCode(hAdjustmentApply.getAadAfterProjectCode());
        hInventoryCreate.setItyProName(hAdjustmentApply.getAadAfterProjectName());
        hInventoryCreate.setItyPart(hAdjustmentApply.getAadAfterPart());
        hInventoryCreate.setItyTrackCode(hAdjustmentApply.getAadAfterTrackCode());
        hInventoryCreate.setItyUnit(hAdjustmentApply.getAadAfterUnit());
        hInventoryCreate.setItyBrand(hAdjustmentApply.getAadAfterBrand());
        hInventoryCreate.setItyWhiId(hAdjustmentApply.getAadAfterWhiId());
        hInventoryCreate.setItyPosition(hAdjustmentApply.getAadAfterPosition());
        hInventoryCreate.setItySapCode(hAdjustmentApply.getAadAfterSapMatCode());
        hInventoryCreate.setItyRemark(hAdjustmentApply.getAadAfterRemark());
        hInventoryCreate.setItyDrawingNumber(hAdjustmentApply.getAadAfterDrawingNo());
//        hInventoryCreate.setItyMakingPiecesStatus(hAdjustmentApply.getAadAfterMakingPiecesStatus());
//        hInventoryCreate.setItyIfFinishedProduct(hAdjustmentApply.getAadAfterIfFinishedProduct());
        hInventoryCreate.setItyMatType(hAdjustmentApply.getAadAfterMatType());
        hInventoryCreate.setItyCaseNo(hAdjustmentApply.getAadAfterCaseNo());
        hInventoryCreate.setItyBoxingDate(hAdjustmentApply.getAadAfterBoxingDate());
        hInventoryCreate.setItyStockType(hAdjustmentApply.getAadAfterStockType());
        hInventoryDao.insert(hInventoryCreate);
        //更新调整表状态
        hAdjustmentApply.setAadReviewDate(new Date());
        hAdjustmentApply.setAadReviewer(user.getName());
        hAdjustmentApply.setAadStatus(3);
        halfInveAdjDao.updateInvCountAppr(hAdjustmentApply);
        logService.saveLog("成品库：审核新增的库存盘点申请", "数量：" + 1 + " 条");
        row++;
        return row;
    }

    /**
     * 审核通过库存盘点
     * 根据盘点明细库存外键ity_id,更改库存信息，库存表ity_row更改为入库单id,调整单状态置为3
     * 插入出入库单
     * 更新库存
     * 更新盘点调整信息
     * @param hAdjustmentApply 盘点信息
     * @param hInventory 库存信息
     * @return 盘点通过条数
     */
    private int hCountApprAagree(HalfInveAdjModel hAdjustmentApply, HInventoryModel hInventory) {
        int row = 0;
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        HInventory hInventoryUpdate = new HInventory();
        //出入库子表插入出库信息
        HOrderDetailModel record = inserthOrderDetail(hAdjustmentApply);
        hOrderDetailDao.insert(record);
        //按ID更新库存
        hInventoryUpdate.setItyNum(hInventory.getItyNum() + hAdjustmentApply.getAjaDeOvNum());
        hInventoryUpdate.setItyAvaNum(hInventory.getItyAvaNum() + hAdjustmentApply.getAjaDeOvNum());
        hInventoryUpdate.setItyRowNo(record.getOdaId());
        hInventoryDao.updateByPrimaryKeySelective(hInventoryUpdate);
        //更新调整表状态
        hAdjustmentApply.setAadReviewDate(new Date());
        hAdjustmentApply.setAadReviewer(user.getName());
        hAdjustmentApply.setAadStatus(3);
        halfInveAdjDao.updateInvCountAppr(hAdjustmentApply);
        logService.saveLog("成品库：审核库存盘点申请", "数量：" + 1 + " 条");
        row++;
        return row;
    }

    /**
     * 审核通过ity_id库存表外键为空调用此方法，调整单状态置为3
     * 插入出入子单出入库信息
     * 插入库存信息
     * 更新调整单信息
     * @param hAdjustmentApply 调整信息
     * @return 库存调整条数
     */
    private int hAdjNumApprAagree(HalfInveAdjModel hAdjustmentApply) {
        int row = 0;
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        //出入库子表插入入库信息
        HOrderDetailModel record = inserthOrderDetail(hAdjustmentApply);
        hOrderDetailDao.insert(record);
        //插入库存
        HInventory order = insertHInventory(hAdjustmentApply,record);
        hInventoryDao.insert(order);
        //更新调整表状态
        hAdjustmentApply.setAadReviewDate(new Date());
        hAdjustmentApply.setAadReviewer(user.getName());
        hAdjustmentApply.setAadStatus(3);
        halfInveAdjDao.updateInvAdjAppr(hAdjustmentApply);
        logService.saveLog("成品库：审核库存调整申请", "数量：" + 1 + " 条");
        row++;
        return row;
    }

    /**
     * 审核通过更改库存调用此方法
     * 更改库存的调整，第一条调整单信息中的调整单外键ity_id不为空，根据ity_id更改库存信息，调整单状态置为3
     * 插入出入子单出入库信息
     * 更新库存信息
     * 更新调整单信息
     * @param hAdjustmentApply 调整信息
     * @param hInventory 库存信息
     * @return 库存调整条数
     */
    private int hAdjApprAagree(HalfInveAdjModel hAdjustmentApply, HInventoryModel hInventory) {
        int row = 0;
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        //出入库子表插入入库信息
        HOrderDetailModel record = inserthOrderDetail(hAdjustmentApply);
        hOrderDetailDao.insert(record);
        //按ID更新库存
        HInventory hInventoryUpadate = updateHInventory(hAdjustmentApply, hInventory,record);
        hInventoryDao.updateByPrimaryKeySelective(hInventoryUpadate);
        //更新调整表状态
        hAdjustmentApply.setAadReviewDate(new Date());
        hAdjustmentApply.setAadReviewer(user.getName());
        hAdjustmentApply.setAadStatus(3);
        halfInveAdjDao.updateInvAdjAppr(hAdjustmentApply);
        logService.saveLog("成品库：审核库存调整申请", "数量：" + 1 + " 条");
        row++;
        return row;
    }

    /**
     * 新增调整，插入库存
     * 审核通过时，调整单的库存外键，ity_id为空时，插入库存信息
     * @param hAdjustmentApply 调整单库存信息
     * @param hOrderDetail 出入库单信息
     * @return 插入的库存信息
     */
    private HInventory insertHInventory(HalfInveAdjModel hAdjustmentApply, HOrderDetailModel hOrderDetail) {
        HInventory hInventoryInsert = new HInventory();
        hInventoryInsert.setItyMatCode(hAdjustmentApply.getAadAfterMatCode());
        hInventoryInsert.setItyMatName(hAdjustmentApply.getAadAfterMatName());
        hInventoryInsert.setItyProCode(hAdjustmentApply.getAadAfterProjectCode());
        hInventoryInsert.setItyProName(hAdjustmentApply.getAadAfterProjectName());
        hInventoryInsert.setItyPart(hAdjustmentApply.getAadAfterPart());
        hInventoryInsert.setItyNum(hAdjustmentApply.getAadAfterNum());
        hInventoryInsert.setItyAvaNum(hAdjustmentApply.getAadAfterNum());
        hInventoryInsert.setItyWeight(hAdjustmentApply.getAadAfterWeight());
        hInventoryInsert.setItyAvaWeight(hAdjustmentApply.getAadAfterWeight());
        hInventoryInsert.setItyTrackCode(hAdjustmentApply.getAadAfterTrackCode());
        hInventoryInsert.setItyUnit(hAdjustmentApply.getAadAfterUnit());
        hInventoryInsert.setItyPrice(hAdjustmentApply.getAadAfterPrice());
        hInventoryInsert.setItyBrand(hAdjustmentApply.getAadAfterBrand());
        hInventoryInsert.setItyWhiId(hAdjustmentApply.getAadAfterWhiId());
        hInventoryInsert.setItyPosition(hAdjustmentApply.getAadAfterPosition());
        hInventoryInsert.setItySapCode(hAdjustmentApply.getAadAfterSapMatCode());
        hInventoryInsert.setItyRemark(hAdjustmentApply.getAadAfterRemark());
        hInventoryInsert.setItyDrawingNumber(hAdjustmentApply.getAadAfterDrawingNo());
//        hInventoryInsert.setItyMakingPiecesStatus(hAdjustmentApply.getAadAfterMakingPiecesStatus());
//        hInventoryInsert.setItyIfFinishedProduct(hAdjustmentApply.getAadAfterIfFinishedProduct());
        hInventoryInsert.setItyMatType(hAdjustmentApply.getAadAfterMatType());
        hInventoryInsert.setItyRowNo(hOrderDetail.getOdaId());
        hInventoryInsert.setItyCaseNo(hAdjustmentApply.getAadAfterCaseNo());
        hInventoryInsert.setItyBoxingDate(hAdjustmentApply.getAadAfterBoxingDate());
        hInventoryInsert.setItyStockType(hAdjustmentApply.getAadAfterStockType());
        return hInventoryInsert;
    }

    /**
     * 保存从库存选择调整的数量调整
     * 库存物料调整，只更改库存数量，
     * 调整前后的物料信息保存库存信息，调整前数量存库存数量，调整后数量存前端返回的库存数量，调整单生成后状态为1
     * @param inventory 库存信息
     * @param hInventory 前端返回的库存信息
     * @return 插入的调整单信息
     */
    private HalfInveAdjModel hAdjustmentApplyUpdateNum(HInventoryModel inventory, HInventoryModel hInventory) {
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        HalfInveAdjModel hAdjustmentApplyUpdateNum = new HalfInveAdjModel();
        //调整前物料赋值
        hAdjustmentApplyUpdateNum.setAadBeforeMatCode(inventory.getItyMatCode());
        hAdjustmentApplyUpdateNum.setAadBeforeMatName(inventory.getItyMatName());
        hAdjustmentApplyUpdateNum.setAadBeforeProjectCode(inventory.getItyProCode());
        hAdjustmentApplyUpdateNum.setAadBeforeProjectName(inventory.getItyProName());
        hAdjustmentApplyUpdateNum.setAadBeforePart(inventory.getItyPart());
        hAdjustmentApplyUpdateNum.setAadBeforeNum(inventory.getItyNum());
        hAdjustmentApplyUpdateNum.setAadBeforeTrackCode(inventory.getItyTrackCode());
        hAdjustmentApplyUpdateNum.setAadBeforeUnit(inventory.getItyUnit());
        hAdjustmentApplyUpdateNum.setAadBeforePrice(inventory.getItyPrice());
        hAdjustmentApplyUpdateNum.setAadBeforeBrand(inventory.getItyBrand());
        hAdjustmentApplyUpdateNum.setAadBeforeWhiId(inventory.getItyWhiId());
        hAdjustmentApplyUpdateNum.setAadBeforePosition(inventory.getItyPosition());
        hAdjustmentApplyUpdateNum.setAadBeforeSapMatCode(inventory.getItySapCode());
        hAdjustmentApplyUpdateNum.setAadBeforeRemark(inventory.getItyRemark());
        hAdjustmentApplyUpdateNum.setAadBeforeDrawingNo(inventory.getItyDrawingNumber());
//        hAdjustmentApplyUpdateNum.setAadBeforeMakingPiecesStatus(inventory.getItyMakingPiecesStatus());
//        hAdjustmentApplyUpdateNum.setAadBeforeIfFinishedProduct(inventory.getItyIfFinishedProduct());
        hAdjustmentApplyUpdateNum.setAadBeforeMatType(inventory.getItyMatType());
        hAdjustmentApplyUpdateNum.setAadBeforeCaseNo(inventory.getItyCaseNo());
        hAdjustmentApplyUpdateNum.setAadBeforeBoxingDate(inventory.getItyBoxingDate());
        hAdjustmentApplyUpdateNum.setAadBeforeStockType(inventory.getItyStockType());
        hAdjustmentApplyUpdateNum.setAadBeforeWeight(inventory.getItyWeight());
        hAdjustmentApplyUpdateNum.setAadAfterWeight(hInventory.getItyWeight());
        //调整后物料赋值
        hAdjustmentApplyUpdateNum.setAadAfterMatCode(inventory.getItyMatCode());
        hAdjustmentApplyUpdateNum.setAadAfterMatName(inventory.getItyMatName());
        hAdjustmentApplyUpdateNum.setAadAfterProjectCode(inventory.getItyProCode());
        hAdjustmentApplyUpdateNum.setAadAfterProjectName(inventory.getItyProName());
        hAdjustmentApplyUpdateNum.setAadAfterPart(inventory.getItyPart());
        hAdjustmentApplyUpdateNum.setAadAfterNum(hInventory.getItyNum());
        hAdjustmentApplyUpdateNum.setAadAfterTrackCode(hInventory.getItyTrackCode());
        hAdjustmentApplyUpdateNum.setAadAfterUnit(inventory.getItyUnit());
        hAdjustmentApplyUpdateNum.setAadAfterPrice(inventory.getItyPrice());
        hAdjustmentApplyUpdateNum.setAadAfterBrand(inventory.getItyBrand());
        hAdjustmentApplyUpdateNum.setAadAfterWhiId(inventory.getItyWhiId());
        hAdjustmentApplyUpdateNum.setAadAfterPosition(inventory.getItyPosition());
        hAdjustmentApplyUpdateNum.setAadAfterSapMatCode(inventory.getItySapCode());
        hAdjustmentApplyUpdateNum.setAadAfterRemark(inventory.getItyRemark());
        hAdjustmentApplyUpdateNum.setAadAfterDrawingNo(inventory.getItyDrawingNumber());
//        hAdjustmentApplyUpdateNum.setAadAfterMakingPiecesStatus(inventory.getItyMakingPiecesStatus());
//        hAdjustmentApplyUpdateNum.setAadAfterIfFinishedProduct(inventory.getItyIfFinishedProduct());
        hAdjustmentApplyUpdateNum.setAadAfterMatType(inventory.getItyMatType());
        hAdjustmentApplyUpdateNum.setAadAfterCaseNo(inventory.getItyCaseNo());
        hAdjustmentApplyUpdateNum.setAadAfterBoxingDate(inventory.getItyBoxingDate());
        hAdjustmentApplyUpdateNum.setAadAfterStockType(inventory.getItyStockType());
        hAdjustmentApplyUpdateNum.setAadAfterSpecialInstructions(hInventory.getItyCause());
        hAdjustmentApplyUpdateNum.setAadApplicant(user.getName());
        hAdjustmentApplyUpdateNum.setAadApplDate(new Date());
        hAdjustmentApplyUpdateNum.setAadStatus(1);
        hAdjustmentApplyUpdateNum.setAadItyId(inventory.getItyId());
        hAdjustmentApplyUpdateNum.setAadAdjOrderCode(createAdjOrderCode());
        return hAdjustmentApplyUpdateNum;
    }

    /**
     * 更改库存的调整，生成第二条调整单信息
     * 从库存表内选择库存物料变更，插入库存调整变更的第二条库存调整单信息
     * 将前端返回的库存信息存入第二条调整单信息内，数量由0 -> x; 本条调整单号置为第一条调整单的单号，调整单生成后状态为1
     * @param hInventory 前端返回的库存信息
     * @param hAdjustmentApplyDetail 第一条调整单信息
     * @return 调整单后单的信息
     */
    private HalfInveAdjModel hAdjustmentApplyInsertInc(HInventoryModel hInventory, HalfInveAdjModel hAdjustmentApplyDetail) {
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        HalfInveAdjModel hAdjustmentApplyInsertInc = new HalfInveAdjModel();
        //更改前物料赋值
        hAdjustmentApplyInsertInc.setAadBeforeMatCode("1111111111");
        hAdjustmentApplyInsertInc.setAadBeforeMatName(hInventory.getItyMatName());
        hAdjustmentApplyInsertInc.setAadBeforeProjectCode(hInventory.getItyProCode());
        hAdjustmentApplyInsertInc.setAadBeforeProjectName(hInventory.getItyProName());
        hAdjustmentApplyInsertInc.setAadBeforePart(hInventory.getItyPart());
        hAdjustmentApplyInsertInc.setAadBeforeNum(Double.valueOf(0));
        hAdjustmentApplyInsertInc.setAadBeforeTrackCode(hInventory.getItyTrackCode());
        hAdjustmentApplyInsertInc.setAadBeforeUnit(hInventory.getItyUnit());
        hAdjustmentApplyInsertInc.setAadBeforePrice(hInventory.getItyPrice());
        hAdjustmentApplyInsertInc.setAadBeforeBrand(hInventory.getItyBrand());
        hAdjustmentApplyInsertInc.setAadBeforeWhiId(hInventory.getItyWhiId());
        hAdjustmentApplyInsertInc.setAadBeforePosition(hInventory.getItyPosition());
        hAdjustmentApplyInsertInc.setAadBeforeSapMatCode(hInventory.getItySapCode());
        hAdjustmentApplyInsertInc.setAadBeforeRemark(hInventory.getItyRemark());
        hAdjustmentApplyInsertInc.setAadBeforeDrawingNo(hInventory.getItyDrawingNumber());
//        hAdjustmentApplyInsertInc.setAadBeforeMakingPiecesStatus(hInventory.getItyMakingPiecesStatus());
//        hAdjustmentApplyInsertInc.setAadBeforeIfFinishedProduct(hInventory.getItyIfFinishedProduct());
        hAdjustmentApplyInsertInc.setAadBeforeMatType(hInventory.getItyMatType());
        hAdjustmentApplyInsertInc.setAadBeforeCaseNo(hInventory.getItyCaseNo());
        hAdjustmentApplyInsertInc.setAadBeforeBoxingDate(hInventory.getItyBoxingDate());
        hAdjustmentApplyInsertInc.setAadBeforeStockType(hInventory.getItyStockType());
        hAdjustmentApplyInsertInc.setAadBeforeWeight(Double.valueOf(0));
        hAdjustmentApplyInsertInc.setAadAfterWeight(hInventory.getItyWeight());
        //更改后物料赋值
        hAdjustmentApplyInsertInc.setAadAfterMatCode("1111111111");
        hAdjustmentApplyInsertInc.setAadAfterMatName(hInventory.getItyMatName());
        hAdjustmentApplyInsertInc.setAadAfterProjectCode(hInventory.getItyProCode());
        hAdjustmentApplyInsertInc.setAadAfterProjectName(hInventory.getItyProName());
        hAdjustmentApplyInsertInc.setAadAfterPart(hInventory.getItyPart());
        hAdjustmentApplyInsertInc.setAadAfterNum(hInventory.getItyNum());
        hAdjustmentApplyInsertInc.setAadAfterTrackCode(hInventory.getItyTrackCode());
        hAdjustmentApplyInsertInc.setAadAfterUnit(hInventory.getItyUnit());
        hAdjustmentApplyInsertInc.setAadAfterPrice(hInventory.getItyPrice());
        hAdjustmentApplyInsertInc.setAadAfterBrand(hInventory.getItyBrand());
        hAdjustmentApplyInsertInc.setAadAfterWhiId(hInventory.getItyWhiId());
        hAdjustmentApplyInsertInc.setAadAfterPosition(hInventory.getItyPosition());
        hAdjustmentApplyInsertInc.setAadAfterSapMatCode(hInventory.getItySapCode());
        hAdjustmentApplyInsertInc.setAadAfterRemark(hInventory.getItyRemark());
        hAdjustmentApplyInsertInc.setAadAfterDrawingNo(hInventory.getItyDrawingNumber());
//        hAdjustmentApplyInsertInc.setAadAfterMakingPiecesStatus(hInventory.getItyMakingPiecesStatus());
//        hAdjustmentApplyInsertInc.setAadAfterIfFinishedProduct(hInventory.getItyIfFinishedProduct());
        hAdjustmentApplyInsertInc.setAadAfterMatType(hInventory.getItyMatType());
        hAdjustmentApplyInsertInc.setAadAfterCaseNo(hInventory.getItyCaseNo());
        hAdjustmentApplyInsertInc.setAadAfterBoxingDate(hInventory.getItyBoxingDate());
        hAdjustmentApplyInsertInc.setAadAfterStockType(hInventory.getItyStockType());
        //状态等赋值
        hAdjustmentApplyInsertInc.setAadAfterSpecialInstructions(hInventory.getItyCause());
        hAdjustmentApplyInsertInc.setAadApplicant(user.getName());
        hAdjustmentApplyInsertInc.setAadApplDate(new Date());
        hAdjustmentApplyInsertInc.setAadStatus(1);
        hAdjustmentApplyInsertInc.setAadAdjOrderCode(hAdjustmentApplyDetail.getAadAdjOrderCode());
        hAdjustmentApplyInsertInc.setAadBeAfFlag(2);
        return hAdjustmentApplyInsertInc;
    }

    /**
     * 更改库存的调整，生成第一条调整单信息
     * 从库存表内选择库存物料变更，插入库存调整变更的第一条库存调整单信息，调整单生成后状态为1
     * 将库存信息存入第一条调整单信息内，数量由x ->0;
     * @param inventory 库存信息
     * @param hInventory 前端返回调整的库存信息
     * @return 调整单前单的信息
     */
    private HalfInveAdjModel hAdjustmentApplyInsertDec(HInventoryModel inventory, HInventoryModel hInventory) {
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        HalfInveAdjModel hAdjustmentApplyInsertDec = new HalfInveAdjModel();
        //更改前物料赋值
        hAdjustmentApplyInsertDec.setAadBeforeMatCode(inventory.getItyMatCode());
        hAdjustmentApplyInsertDec.setAadBeforeMatName(inventory.getItyMatName());
        hAdjustmentApplyInsertDec.setAadBeforeProjectCode(inventory.getItyProCode());
        hAdjustmentApplyInsertDec.setAadBeforeProjectName(inventory.getItyProName());
        hAdjustmentApplyInsertDec.setAadBeforePart(inventory.getItyPart());
        hAdjustmentApplyInsertDec.setAadBeforeNum(inventory.getItyNum());
        hAdjustmentApplyInsertDec.setAadBeforeWeight(inventory.getItyWeight());
        hAdjustmentApplyInsertDec.setAadBeforeTrackCode(inventory.getItyTrackCode());
        hAdjustmentApplyInsertDec.setAadBeforeUnit(inventory.getItyUnit());
        hAdjustmentApplyInsertDec.setAadBeforePrice(inventory.getItyPrice());
        hAdjustmentApplyInsertDec.setAadBeforeBrand(inventory.getItyBrand());
        hAdjustmentApplyInsertDec.setAadBeforeWhiId(inventory.getItyWhiId());
        hAdjustmentApplyInsertDec.setAadBeforePosition(inventory.getItyPosition());
        hAdjustmentApplyInsertDec.setAadBeforeSapMatCode(inventory.getItySapCode());
        hAdjustmentApplyInsertDec.setAadBeforeRemark(inventory.getItyRemark());
        hAdjustmentApplyInsertDec.setAadBeforeDrawingNo(inventory.getItyDrawingNumber());
//        hAdjustmentApplyInsertDec.setAadBeforeMakingPiecesStatus(inventory.getItyMakingPiecesStatus());
//        hAdjustmentApplyInsertDec.setAadBeforeIfFinishedProduct(inventory.getItyIfFinishedProduct());
        hAdjustmentApplyInsertDec.setAadBeforeMatType(inventory.getItyMatType());
        hAdjustmentApplyInsertDec.setAadBeforeCaseNo(inventory.getItyCaseNo());
        hAdjustmentApplyInsertDec.setAadBeforeBoxingDate(inventory.getItyBoxingDate());
        hAdjustmentApplyInsertDec.setAadBeforeStockType(inventory.getItyStockType());
        //更改后物料赋值
        hAdjustmentApplyInsertDec.setAadAfterMatCode(inventory.getItyMatCode());
        hAdjustmentApplyInsertDec.setAadAfterMatName(inventory.getItyMatName());
        hAdjustmentApplyInsertDec.setAadAfterProjectCode(inventory.getItyProCode());
        hAdjustmentApplyInsertDec.setAadAfterProjectName(inventory.getItyProName());
        hAdjustmentApplyInsertDec.setAadAfterPart(inventory.getItyPart());
        hAdjustmentApplyInsertDec.setAadAfterNum(Double.valueOf(0));
        hAdjustmentApplyInsertDec.setAadAfterWeight(Double.valueOf(0));
        hAdjustmentApplyInsertDec.setAadAfterTrackCode(inventory.getItyTrackCode());
        hAdjustmentApplyInsertDec.setAadAfterUnit(inventory.getItyUnit());
        hAdjustmentApplyInsertDec.setAadAfterPrice(inventory.getItyPrice());
        hAdjustmentApplyInsertDec.setAadAfterBrand(inventory.getItyBrand());
        hAdjustmentApplyInsertDec.setAadAfterWhiId(inventory.getItyWhiId());
        hAdjustmentApplyInsertDec.setAadAfterPosition(inventory.getItyPosition());
        hAdjustmentApplyInsertDec.setAadAfterSapMatCode(inventory.getItySapCode());
        hAdjustmentApplyInsertDec.setAadAfterRemark(inventory.getItyRemark());
        hAdjustmentApplyInsertDec.setAadAfterDrawingNo(inventory.getItyDrawingNumber());
//        hAdjustmentApplyInsertDec.setAadAfterMakingPiecesStatus(inventory.getItyMakingPiecesStatus());
//        hAdjustmentApplyInsertDec.setAadAfterIfFinishedProduct(inventory.getItyIfFinishedProduct());
        hAdjustmentApplyInsertDec.setAadAfterMatType(inventory.getItyMatType());
        hAdjustmentApplyInsertDec.setAadAfterCaseNo(inventory.getItyCaseNo());
        hAdjustmentApplyInsertDec.setAadAfterBoxingDate(inventory.getItyBoxingDate());
        hAdjustmentApplyInsertDec.setAadAfterStockType(inventory.getItyStockType());
        //状态等赋值
        hAdjustmentApplyInsertDec.setAadAfterSpecialInstructions(hInventory.getItyCause());
        hAdjustmentApplyInsertDec.setAadItyId(hInventory.getItyId());
        hAdjustmentApplyInsertDec.setAadApplicant(user.getName());
        hAdjustmentApplyInsertDec.setAadApplDate(new Date());
        hAdjustmentApplyInsertDec.setAadStatus(1);
        hAdjustmentApplyInsertDec.setAadAdjOrderCode(createAdjOrderCode());
        hAdjustmentApplyInsertDec.setAadBeAfFlag(1);
        return hAdjustmentApplyInsertDec;
    }

    /**
     * 更新库存信息
     * 审核通过时，按照调整表ity_id外键，更新库存物料信息
     * ItyNum在库数量和ItyAvaNum可用数量都是用实时库存数量-变化差额
     * ItyRowNo为出入库单id
     * @param hAdjustmentApplyDetail 调整单信息
     * @param hInventory 库存信息
     * @param hOrderDetail 出入库单信息
     * @return 更新的库存信息
     */
    private HInventory updateHInventory(HalfInveAdjModel hAdjustmentApplyDetail, HInventoryModel hInventory, HOrderDetailModel hOrderDetail) {
        HInventory hInventoryUpdate = new HInventory();
        hInventoryUpdate.setItyId(hAdjustmentApplyDetail.getAadItyId());
//        hInventoryUpdate.setItyMatCode(hAdjustmentApplyDetail.getAadAfterMatCode());
//        hInventoryUpdate.setItyMatName(hAdjustmentApplyDetail.getAadAfterMatName());
//        hInventoryUpdate.setItyProCode(hAdjustmentApplyDetail.getAadAfterProjectCode());
//        hInventoryUpdate.setItyProName(hAdjustmentApplyDetail.getAadAfterProjectName());
//        hInventoryUpdate.setItyPart(hAdjustmentApplyDetail.getAadAfterPart());
        hInventoryUpdate.setItyNum(hInventory.getItyNum() + (hAdjustmentApplyDetail.getAadAfterNum() - hAdjustmentApplyDetail.getAadBeforeNum()));
        hInventoryUpdate.setItyAvaNum(hInventory.getItyAvaNum() + (hAdjustmentApplyDetail.getAadAfterNum() - hAdjustmentApplyDetail.getAadBeforeNum()));
//        hInventoryUpdate.setItyTrackCode(hAdjustmentApplyDetail.getAadAfterTrackCode());
        if(hAdjustmentApplyDetail.getAadAfterMatType() == 4){
            hInventoryUpdate.setItyWeight(hInventory.getItyWeight() + (hAdjustmentApplyDetail.getAadAfterWeight() - hAdjustmentApplyDetail.getAadBeforeWeight()));
            hInventoryUpdate.setItyAvaWeight(hInventory.getItyAvaWeight() + (hAdjustmentApplyDetail.getAadAfterWeight() - hAdjustmentApplyDetail.getAadBeforeWeight()));
        }
        hInventoryUpdate.setItyUnit(hAdjustmentApplyDetail.getAadAfterUnit());
        hInventoryUpdate.setItyPrice(hAdjustmentApplyDetail.getAadAfterPrice());
//        hInventoryUpdate.setItyBrand(hAdjustmentApplyDetail.getAadAfterBrand());
//        hInventoryUpdate.setItyWhiId(hAdjustmentApplyDetail.getAadAfterWhiId());
//        hInventoryUpdate.setItyPosition(hAdjustmentApplyDetail.getAadAfterPosition());
//        hInventoryUpdate.setItySapCode(hAdjustmentApplyDetail.getAadAfterSapMatCode());
//        hInventoryUpdate.setItyRemark(hAdjustmentApplyDetail.getAadAfterRemark());
//        hInventoryUpdate.setItyDrawingNumber(hAdjustmentApplyDetail.getAadAfterDrawingNo());
//        hInventoryUpdate.setItyMakingPiecesStatus(hAdjustmentApplyDetail.getAadAfterMakingPiecesStatus());
//        hInventoryUpdate.setItyIfFinishedProduct(hAdjustmentApplyDetail.getAadAfterIfFinishedProduct());
//        hInventoryUpdate.setItyMatType(hAdjustmentApplyDetail.getAadAfterMatType());
//        hInventoryUpdate.setItyRowNo(hOrderDetail.getOdaId());
//        hInventoryUpdate.setItyCaseNo(hAdjustmentApplyDetail.getAadAfterCaseNo());
//        hInventoryUpdate.setItyBoxingDate(hAdjustmentApplyDetail.getAadAfterBoxingDate());
//        hInventoryUpdate.setItyStockType(hAdjustmentApplyDetail.getAadAfterStockType());
        return hInventoryUpdate;
    }

    /**
     * 出入库子单插入
     * 审核通过时，插入出入库子单信息
     * @param hAdjustmentApplyDetail 调整单物料信息
     * @return hOrderDetailInsert 插入的出入库子单信息
     */
    private HOrderDetailModel inserthOrderDetail(HalfInveAdjModel hAdjustmentApplyDetail) {
        HOrderDetailModel hOrderDetailInsert = new HOrderDetailModel();
        hOrderDetailInsert.setOdaTrackCode(hAdjustmentApplyDetail.getAadAfterTrackCode());
        hOrderDetailInsert.setOdaProCode(hAdjustmentApplyDetail.getAadAfterProjectCode());
        hOrderDetailInsert.setOdaProName(hAdjustmentApplyDetail.getAadAfterProjectName());
        hOrderDetailInsert.setOdaPart(hAdjustmentApplyDetail.getAadAfterPart());
        hOrderDetailInsert.setOdaMatCode(hAdjustmentApplyDetail.getAadAfterMatCode());
        hOrderDetailInsert.setOdaMatName(hAdjustmentApplyDetail.getAadAfterMatName());
        if (hAdjustmentApplyDetail.getAadBeforeNum() == null){
            hAdjustmentApplyDetail.setAadBeforeNum((double) 0);
        }
        hOrderDetailInsert.setOdaNum(hAdjustmentApplyDetail.getAadAfterNum()-hAdjustmentApplyDetail.getAadBeforeNum());
        hOrderDetailInsert.setOdaWhiId(hAdjustmentApplyDetail.getAadAfterWhiId());
        hOrderDetailInsert.setOdaPosition(hAdjustmentApplyDetail.getAadAfterPosition());
        if (hAdjustmentApplyDetail.getAadAfterPrice() == null){
            hAdjustmentApplyDetail.setAadAfterPrice((double) 0);
        }
        hOrderDetailInsert.setOdaPrice(hAdjustmentApplyDetail.getAadAfterPrice());
        hOrderDetailInsert.setOdaUnit(hAdjustmentApplyDetail.getAadAfterUnit());
        if (hAdjustmentApplyDetail.getAadAfterMatType() == 4){
            hOrderDetailInsert.setOdaWeight(hAdjustmentApplyDetail.getAadAfterWeight() - hAdjustmentApplyDetail.getAadBeforeWeight());
            hOrderDetailInsert.setOdaAmount(hOrderDetailInsert.getOdaWeight()*hAdjustmentApplyDetail.getAadAfterPrice());
        }else {
            hOrderDetailInsert.setOdaAmount(hAdjustmentApplyDetail.getAadAfterPrice() * (hAdjustmentApplyDetail.getAadAfterNum()-hAdjustmentApplyDetail.getAadBeforeNum()));
        }
        hOrderDetailInsert.setOdaBrand(hAdjustmentApplyDetail.getAadAfterBrand());
        hOrderDetailInsert.setOdaMatType(hAdjustmentApplyDetail.getAadAfterMatType());
        hOrderDetailInsert.setOdaRemark(hAdjustmentApplyDetail.getAadAfterRemark());
        hOrderDetailInsert.setOdaStorageDate(new Date());
        hOrderDetailInsert.setOdaDrawingCode(hAdjustmentApplyDetail.getAadAfterDrawingNo());
//        hOrderDetailInsert.setOdaMakingPiecesStatus(hAdjustmentApplyDetail.getAadAfterMakingPiecesStatus());
//        hOrderDetailInsert.setOdaIfFinishedProduct(hAdjustmentApplyDetail.getAadAfterIfFinishedProduct());
        hOrderDetailInsert.setOdaCaseNo(hAdjustmentApplyDetail.getAadAfterCaseNo());
        hOrderDetailInsert.setOdaBoxingDate(hAdjustmentApplyDetail.getAadAfterBoxingDate());
        hOrderDetailInsert.setOdaStockType(hAdjustmentApplyDetail.getAadAfterStockType());
        hOrderDetailInsert.setOdaSapCode(hAdjustmentApplyDetail.getAadAfterSapMatCode());
        return hOrderDetailInsert;
    }

    /**
     * 查看库存盘点
     * 审核库存调整时，查看申请盘点信息和物料库存详细信息
     * @param id 盘点物料id
     * @return 物料盘点信息和库存详细信息
     */
    public HalfInveAdjModel findInvCountById(Integer id) {
        return halfInveAdjDao.selectHalfInveCountById(id);
    }

    /**
     * 生成调整单号
     * 保存库存调整时，调用此方法，生成调整单
     * 调整单编号生成规则：HADJ200317001
     * 首先查找当日是否有调整单，如果当日没有调整单，则生成第一条调整单；如果已有调整单，则查找最后一个调整单号，按顺序生成下一个调整单号
     * @return 调整单号
     */
    public String createAdjOrderCode(){
        // 取得当日零时时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date date = calendar.getTime();
        // 查询当日是否有调整单记录
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String date1 = dateFormat.format(date);
        Date date2 = null;
        try {
            date2 = dateFormat.parse(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<HalfInveAdjModel> halfInveAdjModel = halfInveAdjDao.findAdjByDate(date2);
        String adjOrderCode; // 调整单编号生成规则：HADJ200317001

        if (halfInveAdjModel.size() == 0) //如果当日没有调整单，则生成第一条
        {
            adjOrderCode = "HADJ" + dateFormat.format(new Date()).substring(2,8) + "001";
        } else //如果已有调整单，则查找最后一个调整单号，按顺序生成下一个调整单号
        {
            adjOrderCode = "HADJ" + dateFormat.format(new Date()).substring(2,8) + String.format("%03d", Integer.parseInt(halfInveAdjModel.get(halfInveAdjModel.size()-1).getAadAdjOrderCode().substring(11, 13)) + 1);
        }
        return adjOrderCode;
    }

    /**
     * 查找库房列表
     * 用于下拉框选项选择库房
     * 只查找成品库库房
     * @return 库房列表
     */
    public Object findWhiId() {
        TWarehouseInfoExample ex = new TWarehouseInfoExample();
        TWarehouseInfoExample.Criteria criteria = ex.createCriteria();
        criteria.andWhiTypeEqualTo("成品库");
        List <TWarehouseInfo> warehouseInfos = tWarehouseInfoDao.selectByExample(ex);
        return warehouseInfos;
    }

    public Boolean codeExist(HalfInveAdjModel param) {
        Boolean boolExist;
        List<ProjectStatusModel> projectStatusModels = halfInveAdjDao.findProjectByCode(param);
        if (projectStatusModels == null || projectStatusModels.size() == 0) {
            boolExist = false;
        } else {
            boolExist = true;
        }
        return boolExist;
    }
}